import logging

from selenium import webdriver
from selenium.webdriver.common.by import By
from options import options
from time import sleep

class Asms:
    def __init__(self,dr):
        self.dr = dr
    def login(self,id,username,password,identify):
        self.dr.find_element(By.ID, 'taskId').send_keys(id)
        sleep(1)
        self.dr.find_element(By.ID, 'loginName').send_keys(username)
        sleep(1)
        self.dr.find_element(By.ID, 'password').send_keys(password)
        sleep(1)
        self.dr.find_element(By.ID, 'vericode').send_keys(identify)
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="fmedit"]/div[2]/div[6]/input').click()
        sleep(3)
    def logout(self):
        self.dr.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/a[3]').click()
        sleep(3)
    def asset_add(self,name):
        self.dr.find_element(By.XPATH,'//*[@id="leftmenu_asset_manage"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'button_true').click()
        sleep(1)
        self.dr.find_element(By.ID, 'title').send_keys(name)
        sleep(2)
        self.dr.find_element(By.ID, 'providerId').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="providerId"]/option[3]').click()
        sleep(1)
        self.dr.find_element(By.ID, 'storageId').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="storageId"]/option[3]').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]/div/div[1]').click()
        sleep(1)
        # self.dr.switch_to.alert.accept()
        # sleep(3)
    def asset_update(self,name):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_manage"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]').click()
        sleep(1)
        self.dr.find_element(By.ID, 'title').clear()
        self.dr.find_element(By.ID, 'title').send_keys(name)
        sleep(2)
        self.dr.find_element(By.ID, 'storageId').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="storageId"]/option[3]').click()
        sleep(1)
        self.dr.find_element(By.ID, 'providerId').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="providerId"]/option[3]').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]').click()
        sleep(1)
        # self.dr.switch_to.alert.accept()
        # sleep(3)
    def asset_lend(self,massage):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_manage"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[2]').click()
        sleep(1)
        self.dr.find_element(By.ID, 'departId').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="departId"]/option[3]').click()
        sleep(1)
        self.dr.find_element(By.ID, 'remark').send_keys(massage)
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="fm_new"]/div/div[1]').click()
        sleep(1)
        # self.dr.switch_to.alert.accept()
        # sleep(3)
    def asset_borrow(self):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_manage"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[2]').click()
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[2]/div[1]').click()
        sleep(3)
        # self.dr.switch_to.alert.accept()
        # sleep(3)

    def personal_tel(self,tel):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_user"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.ID, 'phone').clear()
        sleep(1)
        self.dr.find_element(By.ID, 'phone').send_keys(tel)
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'button_fujian').click()
        sleep(1)
        # text = self.dr.switch_to.alert.text
        # self.dr.switch_to.alert.accept()
        # sleep(3)
        # return text
    def personal_pwd(self,oldpwd,newpwd,newpwd1):
        self.dr.find_element(By.LINK_TEXT, '修改密码').click()
        sleep(1)
        self.dr.find_element(By.ID, 'oldPassword').send_keys(oldpwd)
        sleep(1)
        self.dr.find_element(By.ID, 'newPassword1').send_keys(newpwd)
        sleep(1)
        self.dr.find_element(By.ID, 'newPassword2').send_keys(newpwd1)
        sleep(1)
        self.dr.find_element(By.XPATH, '//*[@id="cboxLoadedContent"]/div/div/div[2]/div[4]/div[1]').click()
        sleep(1)
        # text = self.dr.switch_to.alert.text
        # self.dr.switch_to.alert.accept()
        # sleep(3)
        # return text
    def address_find(self):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_storage"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.ID, 'status').click()
        sleep(1)
        self.dr.find_element(By.CSS_SELECTOR, '#status > option:nth-child(2)').click()
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'input_gry').send_keys('总经理办公室')
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'search_button').click()
        sleep(1)
    def apply_find(self):
        self.dr.find_element(By.XPATH, '//*[@id="leftmenu_asset_provider"]/div[1]/a/img').click()
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'select_gry').click()
        sleep(1)
        self.dr.find_element(By.CSS_SELECTOR, '#fmsearch > div:nth-child(1) > select > option:nth-child(3)').click()
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'input_gry').send_keys('北京合力科技有限公司')
        sleep(1)
        self.dr.find_element(By.CLASS_NAME, 'search_button').click()
        sleep(1)



if __name__ == '__main__':
    url=r'http://172.16.22.105:8080/bsams/front/login.do'
    dr=webdriver.Chrome(options=options)
    dr.get(url)
    dr.maximize_window()
    id='1'
    username='test11'
    password='1234567'
    tel='18638798023'
    asms=Asms(dr)
    asms.login(id,username,password,identify='shtd')
    # asms.asset_add()
    # asms.asset_update()
    # asms.logout()
    # asms.asset_lend()
    # asms.asset_borrow()
    # asms.personal_tel()
    # asms.personal_pwd()
    # asms.address_find()
    # asms.apply_find()
